package com.leyou.item.service.service.impl;

import com.leyou.common.bean.Result;
import com.leyou.common.bean.ResultCode;
import com.leyou.item.inter.dto.CategoryUpdateDTO;
import com.leyou.item.inter.pojo.Brand;
import com.leyou.item.inter.pojo.Category;
import com.leyou.item.service.mapper.CategoryMapper;
import com.leyou.item.service.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zqq
 * @ClassName CategoryServiceImpl
 * @Description
 * @date 2020/3/3-15:47
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result<List<Category>> queryCategoriesByPid(Long pid) {
        // 初始化example对象
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        //设置实体类中的属性名，否则报错
        criteria.andEqualTo("parentId",pid);
        example.setOrderByClause("sort asc");
        List<Category> list = null;
        try {
            list =  this.categoryMapper.selectByExample(example);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(list);
    }

    public Result<List<Category>> queryByBrandId(Long bid) {
        List<Category> list = null;
        try {
            list = this.categoryMapper.queryByBrandId(bid);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(list);
    }

    @Override
    public Result<List<String>> queryNamesByIds(List<Long> ids) {
        List<Category> list = null;
        try {
            list = this.categoryMapper.selectByIdList(ids);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        List<String> names = new ArrayList<>();
        for (Category category : list) {
            names.add(category.getName());
        }
        return Result.newSuccess(names);
    }

    @Override
    public Result<List<Category>> queryListByCid() {
        Category category = new Category();
        category.setParentId(0L);
        List<Category> categoryList = null;
        try {
            categoryList = this.categoryMapper.select(category);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        for (Category two : categoryList) {
            category.setParentId(two.getId());
            List<Category> twoList = this.categoryMapper.select(category);
            if(twoList.size() != 0)
                two.setList(twoList);
            for (Category three : twoList) {
                category.setParentId(three.getId());
                List<Category> threeList = this.categoryMapper.select(category);
                if(threeList.size() != 0)
                    three.setList(threeList);
            }
        }
        return Result.newSuccess(categoryList);
    }

    @Override
    public Result<List<Category>> queryAllByCid3(Long id) {
        Category c3 = null;
        Category c2 = null;
        Category c1 = null;
        try {
            c3 = this.categoryMapper.selectByPrimaryKey(id);
            c2 = this.categoryMapper.selectByPrimaryKey(c3.getParentId());
            c1 = this.categoryMapper.selectByPrimaryKey(c2.getParentId());
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(Arrays.asList(c1,c2,c3));
    }

    @Override
    public Result updateCategory(CategoryUpdateDTO dto) {
        if(dto.getType() == 1){
            if(dto.getId() == null){
                return Result.newFailure(ResultCode.PARAM_NOT_NULL);
            }
            Category entity = new Category();
            entity.setId(dto.getId());
            entity.setName(dto.getName());
            try {
                this.categoryMapper.updateByPrimaryKeySelective(entity);
            } catch (Exception e) {
                log.error("sql异常", e);
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }
        }else  if(dto.getType() == 0){
            //获取最新的id
            Category bran = null;
            try {
                bran = this.categoryMapper.getNewestId();
            } catch (Exception e) {
                log.error("sql异常", e);
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }
            long id = bran.getId() + 1;
            Category entity = new Category();
            entity.setId(id);
            if(dto.getName() != null)
                entity.setName(dto.getName());
            if(dto.getParentId() != null)
                entity.setParentId(dto.getParentId());
            if(dto.getIsParent() != null)
                entity.setIsParent(dto.getIsParent());
            if(dto.getSort() != null)
                entity.setSort(dto.getSort());
            if(dto.getParentId() != 0){
                Category category = this.categoryMapper.selectByPrimaryKey(dto.getParentId());
                if(category.getParentId() != 0){
                    Category parentCategory = this.categoryMapper.selectByPrimaryKey(category.getParentId());
                    if(parentCategory.getParentId() == 0){
                        Category update = new Category();
                        update.setId(dto.getParentId());
                        update.setIsParent(true);
                        this.categoryMapper.updateByPrimaryKeySelective(update);
                    }else{
                        //添加四级
                        return Result.newFailure(ResultCode.USER_ADD_CATEGORY_ERROR);
                    }
                }
            }
            try {
                this.categoryMapper.insertSelective(entity);
            } catch (Exception e) {
                log.error("sql异常", e);
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }
            return Result.newSuccess(id);
        }
        return Result.newSuccess();
    }

    @Override
    public Result deleteCategory(Long id) {
        Category entity = null;
        try {
            entity = this.categoryMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        try {
            this.categoryMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        //如果没子类就返回，有就得把所有子类删除
        if(!entity.getIsParent()){
            return Result.newSuccess();
        }
        Category dto = new Category();
        dto.setParentId(id);
        List<Category> select = this.categoryMapper.select(dto);
        for (Category category : select) {
            this.categoryMapper.deleteByPrimaryKey(category.getId());
            dto.setParentId(category.getId());
            List<Category> select1 = this.categoryMapper.select(dto);
            for (Category category1 : select1) {
                this.categoryMapper.deleteByPrimaryKey(category1.getId());
            }
        }
        return Result.newSuccess();
    }

    @Override
    public Result<Category> queryCategoryById(Long id) {
        Category category = null;
        try {
            category = this.categoryMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(category);
    }
}
